/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.statweb.action;

import cn.ac.iie.di.wx.statweb.utils.MySQLUtil;
import static cn.ac.iie.di.wx.statweb.utils.ToJsonUtil.toJSONString;
import cn.ac.iie.di.wx.statweb.vo.WarningVo;
import static com.opensymphony.xwork2.Action.SUCCESS;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author Administrator
 */
public class WarningAction {

    static Logger logger = Logger.getLogger(WarningAction.class.getName());

    public String Warning() {
        return "success";
    }

    public String getTableList() throws IOException {
        HttpServletRequest req = ServletActionContext.getRequest();
        req.setCharacterEncoding("UTF-8");
        String start = req.getParameter("start");
        String limit = req.getParameter("limit");
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter pw = response.getWriter();

        List<WarningVo> list = new ArrayList<WarningVo>();
        List<WarningVo> pagelist = new ArrayList<WarningVo>();

        list = MySQLUtil.queryTableByStatus();
        if (!list.isEmpty()) {
            //监测是否有数据
            if (start.equals("index") && limit.equals("period")) {
                pw.print("yes");
                pw.close();
                logger.info("there is a task to be handled");
                return SUCCESS;
            } else {
                pw.print(toJSONString(list, pagelist, start, limit));
                pw.close();
                logger.info("the xml has been sent to web page");
                return SUCCESS;
            }

        } else {
            if (start.equals("index") && limit.equals("period")) {
                pw.print("no");
                pw.close();
                logger.info("there is a task to be handled");
                return SUCCESS;
            } else {
                list.add(new WarningVo("-1", "-1", "get list failure", "0", "0", "1"));
                pw.print(toJSONString(list, pagelist, start, limit));
                pw.close();
                logger.info("get list error");
                return SUCCESS;
            }
        }

    }

    public String updateTableList() throws IOException {
        HttpServletRequest req = ServletActionContext.getRequest();
        req.setCharacterEncoding("UTF-8");
        String start = req.getParameter("start");
        String limit = req.getParameter("limit");
        String id = req.getParameter("id");
        logger.info("update id is :" + id);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter pw = response.getWriter();
        List<WarningVo> list = new ArrayList<WarningVo>();
        List<WarningVo> pagelist = new ArrayList<WarningVo>();

        boolean flag = MySQLUtil.updateById(id);
        if (flag) {
            pw.print(id);
            pw.close();
            logger.info("the xml has been sent to web page");
            return SUCCESS;
        } else {
            pw.print("failure");
            pw.close();
            logger.info("get list error");
            return SUCCESS;
        }

    }

    public String updateRemark() throws IOException, SQLException {
        HttpServletRequest req = ServletActionContext.getRequest();
        req.setCharacterEncoding("UTF-8");
        String start = req.getParameter("start");
        String limit = req.getParameter("limit");
        String id = req.getParameter("id");
        String remark = req.getParameter("remark");
        logger.info("update id is :" + id);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter pw = response.getWriter();
        List<WarningVo> list = new ArrayList<WarningVo>();
        List<WarningVo> pagelist = new ArrayList<WarningVo>();

        boolean flag = MySQLUtil.updateRemarkById(id, remark);
        if (flag) {
            pw.print(id);
            pw.close();
            logger.info("the xml has been sent to web page");
            return SUCCESS;
        } else {
            pw.print("failure");
            pw.close();
            logger.info("get list error");
            return SUCCESS;
        }

    }

    public String batchDelete() throws UnsupportedEncodingException, IOException {
        HttpServletRequest req = ServletActionContext.getRequest();
        req.setCharacterEncoding("UTF-8");
        String success = "success";
        String idArray = req.getParameter("idArray");
        String[] array = idArray.split(";");
        int count=0;
        for (int i = 0; i < array.length; i++) {
            try {
                boolean flag = MySQLUtil.deleteById(array[i]);
                if (!flag) {
                    
                    if (count>0) {
                        success +=array[i]+";";
                    }else{
                        success =array[i]+";";
                        count++;
                    }
                }
            } catch (SQLException ex) {
                success="failure";
                logger.error(ex.getMessage(), ex);
            }
        }
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter pw = response.getWriter();
        pw.print(success);
        pw.close();
        return SUCCESS;
    }
}
